﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.TreatmentHistory
{
    public class PresHistoryStart : PatientBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected PrescriptionInfo _presInfo;
        protected int bCurrentInfo = 0;

        protected void HandleCommon()
        {
            this.AddLocation("治疗记录", "/TreatmentHistory/Index.aspx");
            this.AddLocation("开始治疗", "/TreatmentHistory/HistoryStart.aspx");
            this.SetTitle("开始治疗");

            bCurrentInfo = RequestTools.GetInt("bCur", 0);
            int presId = RequestTools.GetInt("prescriptionid", 0);
            if(presId <= 0)
            {
                this.AddErrLine("参数错误！");
                return;
            }
            _presInfo = PrescriptionBLL.Instance.GetPrescriptionInfo(presId);
            if (_presInfo.Id <= 0)
            {
                this.AddErrLine("参数错误！");
                return;
            }
        }

        protected void HandlePost()
        {
            PrescriptionHistoryInfo info = this.ValidateForm();
            if (info == null)
                return;

            bool result = PatientPrescriptionBLL.Instance.AddPrescriptionHistory(info);
            if (result)
            {
                this.AddMsgLine("添加成功");
            }
            else
            {
                this.AddErrLine("添加失败");
            }

            this.SetUrl("/TreatmentHistory/PrescriptionList.aspx?prescriptionid=" + _presInfo.Id);
            this.SetMetaRefresh();
        }

        protected void HandleGet()
        {
            
        }

        protected PrescriptionHistoryInfo ValidateForm()
        {
            PrescriptionHistoryInfo info = new PrescriptionHistoryInfo();

            info.UsePurpose = RequestTools.GetInt("usepurpose", -1);
            if (info.UsePurpose >= Constants.TreatmentPurpose.Length || info.UsePurpose < 0)
            {
                this.AddErrLine("请填写使用原因");
            }

            // 对时间进行判断
            string value = RequestTools.GetString("startdate");
            if (value == "")
            {
                this.AddErrLine("请填写开始时间");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                this.AddErrLine("开始日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            else if (DateTime.Compare(date, PrData.StartDate) < 0)
            {
                this.AddErrLine("开始日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
                return null;
            }

            //对时间进行检查
            info.StartDate = date;

            //需要对判断是否有相同时间的记录
            if (PatientPrescriptionBLL.Instance.CheckPrescriptionHistory(PrData.Id, _presInfo.Id, info.StartDate))
            {
                this.AddErrLine("是否需要覆盖" + value + "已有的历史记录？");
                return null;
            }

            value = RequestTools.GetString("enddate");
            if (value == "")
            {
                info.EndDate = DateTime.MaxValue ;
            }
            else
            {
                if ( !DateTime.TryParse(value, out date) || date < info.StartDate)
                {
                    this.AddErrLine("错误的时间 " + value);
                    return null;
                }
                info.EndDate = date;
            }

            info.UpdateDate = DateTime.Now;
            info.PrescriptionId = _presInfo.Id;
            info.ParentPrescriptionId = _presInfo.ParentId;
            info.State = Constants.CURRENT_USING;
            info.EndDate = DateTime.MaxValue;
            info.PRId = PrData.Id;
            info.UserId = UserData.Id;

            return info;
        }
    }
}
